package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author atguigu-mqx
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(UserInfo userInfo) {

        //  select * from  user_info where login_name = ? and passwd = ?;
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("login_name",userInfo.getLoginName());
        //  设置密码加密：
        String passwd = userInfo.getPasswd();
        String newPassword = DigestUtils.md5DigestAsHex(passwd.getBytes());
//        String encrypt = MD5.encrypt(passwd);
        userInfoQueryWrapper.eq("passwd",newPassword);

        //  实现类中：可能需要放入缓存，以及生成token 放入cookie 等！
        return userInfoMapper.selectOne(userInfoQueryWrapper);
    }

    @Override
    public UserInfo getUserInfo(String userId) {
        return userInfoMapper.selectById(userId);
    }
}
